/**
 *
 * @author Faith
 */
public class Philosopher extends Thread{
    private int id;
    private int fork1, fork2;
    private Object[] forks;

    public Philosopher(int id, int fork1, int fork2, Object[] forks) {
        this.id = id;
        this.fork1 = fork1;
        this.fork2 = fork2;
        this.forks = forks;
    }

    public void run(){
        status("Ready to eat using forks "+fork1+" and "+fork2);
        pause(); // pause to let others get ready
        while(true){
            status("Picking up fork "+fork1);
            synchronized(forks[fork1]){
                status("Picking up fork "+ fork2);
                synchronized(forks[fork2]){
                    status("Eating");
                }
            }

        }
    }

    private void pause(){
        try{
            sleep(200);
        }catch(InterruptedException e){
            // do nothing
        }
    }

    private void status(String msg){
        System.out.println("Philosopher " + id + ": "+msg);
    }


}
